#include "view_others_stopwatch.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_others_device_info.h"
#include "view_common.h"
#include "app_trace.h"
#include "stopwatch.h"
#include "view_others_stopwatch.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#ifdef NO_STOPWATCH_UI
#else     
typedef enum __stop_watch_type_t
{
    stop_watch_type_main,
    stop_watch_type_other,
} stop_watch_type_t;
static void _disp_show_stopwatch(uint32_t time, int start_y, stop_watch_type_t type)
{
    int font_width = 0;
    int font_interval = 0;
    int point_interval = 0;
    int point_width = 0;
    int ms_interval = 0;
    int x = 0;
    uint8_t const *const *font_tab = 0;
    uint8_t const *point = 0;
    uint32_t temp32 = time;
    uint8_t temp8 = (temp32 % 1000) / 10;
    switch (type)
    {
    case stop_watch_type_main:
        font_width = 16;
        font_interval = 4;
        point_interval = 5;
        point_width = 7;
        ms_interval = 5;
        font_tab = img_j501_TAB;
        point = img_j501_two_point;
        x = 73;
        break;
    case stop_watch_type_other:
        font_width = 14;
        font_interval = 2;
        point_interval = 4;
        point_width = 5;
        ms_interval = 6;
        font_tab = img_j401_TAB;
        point = img_j401_two_point;
        x = 77;
        break;
    default:
        return;
    }

    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval, start_y + 1, img_j201_TAB[temp8 / 10]); //毫秒
    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval + 9, start_y + 1, img_j201_TAB[temp8 % 10]);
    // disp_draw_user_image(142, start_y + 12 + 5, IMAGE_USER_STOPWATCH_TIME_DOT);

    temp32 /= 1000;
    disp_draw_user_image(x + 4 * font_width + 2 * point_width + 2 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //秒
    disp_draw_user_image(x + 5 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 4 * font_width + 1 * point_width + 2 * font_interval + 3 * point_interval, start_y, point);

    temp32 /= 60;
    disp_draw_user_image(x + 2 * font_width + 1 * point_width + 1 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //分
    disp_draw_user_image(x + 3 * font_width + 1 * point_width + 2 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 2 * font_width + 0 * point_width + 1 * font_interval + 1 * point_interval, start_y, point);

    temp32 /= 60;
    disp_draw_user_image(x + 0 * font_width + 0 * point_width + 0 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //时
    disp_draw_user_image(x + 1 * font_width + 0 * point_width + 1 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
}

void view_others_stopwatch_display(void)
{
    if (disp_ctrl.auto_refresh_count == 0)
    {
        //_disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 6, 10, FILE_IMG_STOPWATCH_6_10);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 9, 180, FILE_IMG_E107_timer_9_180);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 24, 14, FILE_IMG_E108_timer_1_24_14);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 24, 73, FILE_IMG_E109_timer_2_24_73);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, 24, 129, FILE_IMG_E110_timer_3_24_129);
        if (disp_ctrl.loop_cnt == 0)
        {
            display_set_update_area(73, 16, 240 - 73, 230 - 16); //!< 73, 75,225, 192
            disp_ctrl.com_data.i_temp[4] = 0;
        }
    }
    else
    {
        if ((disp_ctrl.com_data.i_temp[4] == 0) && (disp_ctrl.loop_cnt == 0))
        {
            display_set_update_area(73, 16, 240 - 73, 230 - 16);
            disp_ctrl.com_data.i_temp[4] = 1;
        }
    }
    // 0x39c7
    disp_draw_rect_color(0, 60, 239, 60, 0x39c7);
    disp_draw_rect_color(0, 116, 239, 116, 0x39c7);
    disp_draw_rect_color(0, 173, 239, 173, 0x39c7);
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = stopwatch_get_catch_time(0);
        disp_ctrl.com_data.i_temp[1] = stopwatch_get_catch_time(1);
        disp_ctrl.com_data.i_temp[2] = stopwatch_get_catch_time(2);
        disp_ctrl.com_data.i_temp[3] = stopwatch_get_catch_time(3);
        time_t time;
        rtc_get_time(&time);
        disp_ctrl.com_data.i_temp[4] = ble_if_connected();
        disp_ctrl.com_data.i_temp[5] = time.hour;
        disp_ctrl.com_data.i_temp[6] = time.minute;
        disp_ctrl.auto_refresh_time = 22;
        disp_ctrl.auto_close_delay = 10000;
    }

    //主时间
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[0], 190, stop_watch_type_main);

    // 1
    if (disp_ctrl.com_data.i_temp[1] != 0)
    {
        _disp_show_stopwatch(disp_ctrl.com_data.i_temp[1], 16, stop_watch_type_other);
    }
    // 2
    if (disp_ctrl.com_data.i_temp[2] != 0)
    {
        _disp_show_stopwatch(disp_ctrl.com_data.i_temp[2], 75, stop_watch_type_other);
    }
    // 3
    if (disp_ctrl.com_data.i_temp[3] != 0)
    {
        _disp_show_stopwatch(disp_ctrl.com_data.i_temp[3], 131, stop_watch_type_other);
    }
}
    
view_event_result_t view_others_stopwatch_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        // stopwatch_reset();
        return view_event_result_handled;
    case event_view_exit:
        stopwatch_stop();
        return view_event_result_handled;
// #ifdef EVENT_NEXT_VIEW
//     case event_move_next_view:
// #endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP:
#endif
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            stopwatch_catch_time();
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
            return view_event_result_handled;
        }
    }
    break;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            return view_event_result_handled;
        }
        break;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
        return view_event_result_handled;
#endif
        // #ifdef EVENT_LEVEL_DOWN
        //     case event_move_level_down:
        //     {
        //         stop_watch_state_t state = stopwatch_state();
        //         if (state == stop_watch_state_start)
        //         {
        //             stopwatch_stop();
        //             display_set_update_request(view_others_stopwatch, 0);
        //             disp_ctrl.area_update_flag = 0;
        //         }
        //         else
        //         {
        //             stopwatch_resume();
        //             display_set_update_request(view_others_stopwatch, 0);
        //             disp_ctrl.area_update_flag = 0;
        //         }
        //         return view_event_result_handled;
        //     }
        // #endif

#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            stopwatch_stop();
            // disp_ctrl.auto_refresh_time = 22;
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        else
        {
            stopwatch_start();
            // disp_ctrl.auto_refresh_time = 22;
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        return view_event_result_handled;
    }
    }
    return view_event_result_not_handled;
}
#endif
